<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'圈列表'),
);
$setting=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'bossmid >'=>0);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_quan_sns',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
        $item['member']=cfc('core')->member_show($item['bossmid']);
		$item['pcd']=json_decode($item['pcd'],true);
		$item['pcd']=$item['pcd']['province'].''.$item['pcd']['city'].''.$item['pcd']['district'];
		$item['itemsum']=pdo_count('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'snsid'=>$item['id']),'id');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'圈名称','type'=>'string'),
			array('name'=>'pcd','title'=>'圈地址','type'=>'string'),
            array('name'=>'member','title'=>'圈主','type'=>'member'),
			array('name'=>'itemsum','title'=>'主题数','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'setboss','title'=>'清空圈主'),
			array('type'=>'href','op'=>'items','title'=>'主题列表'),
		),),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'主题名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setboss'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择圈！','error');
	}
	pdo_update('jy_quan_sns',array('bossmid'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('清空成功！！');
}

if($_RQ['op']=='items'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['snsid'] = intval($_RQ['id']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','snsid','mid','paixu','uniacid','title','logo','onlinetime','status','type','createtime');
	$list=pdo_getslice('jy_quan_item',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$memebr = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),array('nickname','avatar'));
		$item['nickname']=$memebr['nickname'];
		$item['avatar']=$memebr['avatar'];
		$item['snstitle']=pdo_getcolumn('jy_quan_sns',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['snsid']),'title');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'主题名称','type'=>'string'),
			array('name'=>'nickname','title'=>'发布者','type'=>'string'),
			array('name'=>'snstitle','title'=>'来自圈','type'=>'string'),
			//array('name'=>'logo','title'=>'主题封面','type'=>'image'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('type'=>'href','op'=>'jielong','title'=>'接龙记录'),
			array('type'=>'href','op'=>'msg','title'=>'留言记录'),
		),),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'主题名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status'),
		)
	);
	$return['status']=array(array('id'=>0,'title'=>'未显示'),array('id'=>1,'title'=>'显示'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='jielong'){
	$id=intval($_RQ['id']);
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$id);
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_quan_item_jieli',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$memebr = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),array('nickname','avatar'));
		$item['nickname']=$memebr['nickname'];
		$item['avatar']=$memebr['avatar'];
		$item['ptitle']=pdo_getcolumn('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']),'title');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'pid','title'=>'主题ID','type'=>'string'),
			array('name'=>'ptitle','title'=>'主题名称','type'=>'string'),
			array('name'=>'mid','title'=>'用户ID','type'=>'string'),
			array('name'=>'nickname','title'=>'接龙人昵称','type'=>'string'),
			array('name'=>'avatar','title'=>'接龙人头像','type'=>'image'),
			array('name'=>'title','title'=>'接龙语句','type'=>'string'),
			array('name'=>'createtime','title'=>'接龙时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'nickname','width'=>15,'title'=>'接龙人昵称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='msg'){
	$id=intval($_RQ['id']);
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$id);
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_quan_item_msg',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$memebr = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),array('nickname','avatar'));
		$item['nickname']=$memebr['nickname'];
		$item['avatar']=$memebr['avatar'];
		$item['ptitle']=pdo_getcolumn('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']),'title');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'pid','title'=>'主题ID','type'=>'string'),
			array('name'=>'ptitle','title'=>'主题名称','type'=>'string'),
			array('name'=>'mid','title'=>'用户ID','type'=>'string'),
			array('name'=>'nickname','title'=>'留言人昵称','type'=>'string'),
			array('name'=>'avatar','title'=>'留言人头像','type'=>'image'),
			array('name'=>'title','title'=>'留言语句','type'=>'string'),
			array('name'=>'createtime','title'=>'转发时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'nickname','width'=>15,'title'=>'留言人昵称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id=intval($postdata['id']);
	if(!$postdata['title']){
		exi('请填写主题名称！','error');
	}
	//print_r($postdata['content']);exit;
	$pcd = $postdata['pcd'];
	if(!$pcd['province'] || !$pcd['city'] || !$pcd['district']){
		exi('请完善圈所属地址！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'bossmid'=>intval($postdata['bossmid']),
		'pcd'=>ijson_encode($postdata['pcd']),
		'status'=>intval($postdata['status']),
	);
	if($id){
		pdo_update('jy_quan_sns',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_quan_sns',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_quan_item',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}